<script type="text/javascript">
$(document).ready(function(){
    $('#matter-country').autocomplete({
        minLength: 2,
        source: "/matter/get-country-codes",
        focus: function(event, ui){
            return false;
        },
        autoFocus: true,
        select: function( event, ui ) {
                    $( "#matter-country" ).val( ui.item.value );
                    $( "#matter-country-id" ).val( ui.item.id );
                 return false;
        },
        change: function( event, ui ){
                    valid=true;
                    if ( !ui.item ) {
                      valid=false;
                    }
                   if ( !valid ) {
                     // remove invalid value, as it didn't match anything
                      $(this).val( "" );
                      $(this).data( "autocomplete" ).term = "";
                      return false;
                   }
                 return false;
        }
    }).select();
    $('#matter-origin').autocomplete({
        minLength: 2,
        source: "/matter/get-country-codes",
        focus: function(event, ui){
            return false;
        },
        autoFocus: true,
        select: function( event, ui ) {
                    $( "#matter-origin" ).val( ui.item.value );
                    $( "#matter-origin-id" ).val( ui.item.id );
                 return false;
        },
        change: function( event, ui ){
                    valid=true;
                    if ( !ui.item ) {
                      valid=false;
                    }
                   if ( !valid ) {
                     // remove invalid value, as it didn't match anything
                      $(this).val( "" );
                      $(this).data( "autocomplete" ).term = "";
                      return false;
                   }
                 return false;
        }
    });
    $('#matter-type').autocomplete({
        minLength: 1,
        source: "/matter/get-matter-types",
        autoFocus: true,
        focus: function(event, ui){
            return false;
        },
        select: function( event, ui ) {
                    $( "#matter-type" ).val( ui.item.value );
                    $( "#matter-type-id" ).val( ui.item.id );
                 return false;
        },
        change: function( event, ui ){
                    valid=true;
                    if ( !ui.item ) {
                      valid=false;
                    }
                   if ( !valid ) {
                     // remove invalid value, as it didn't match anything
                      $(this).val( "" );
                      $(this).data( "autocomplete" ).term = "";
                      return false;
                   }
                 return false;
        }
    });
    $('#matter-caseref').autocomplete({
        minLength: 3,
        source: "/matter/get-matter-caseref",
        autoFocus: true,
        focus: function(event, ui){
            return false;
        },
        select: function( event, ui ) {
                    $( "#matter-caseref" ).val( ui.item.value );
                    $( "#matter-caseref-id" ).val( ui.item.id );
                 return false;
        },

    });
    $('#matter-responsible').autocomplete({
        minLength: 1,
        source: "/matter/get-all-logins",
        autoFocus: true,
        focus: function(event, ui){
            return false;
        },
        select: function( event, ui ) {
                    $( "#matter-responsible" ).val( ui.item.value );
                 return false;
        },
        change: function( event, ui ){
                    valid=true;
                    if ( !ui.item ) {
                      valid=false;
                    }
                   if ( !valid ) {
                     // remove invalid value, as it didn't match anything
                      $(this).val( "" );
                      $(this).data( "autocomplete" ).term = "";
                      return false;
                   }
                 return false;
        }
    });
    $('#matter-category').autocomplete({
        minLength: 1,
        source: "/matter/get-all-categories",
        autoFocus: true,
        focus: function(event, ui){
            return true;
        },
        select: function( event, ui ) {
                    $( "#matter-category" ).val( ui.item.value );
                    $( "#matter-category-code" ).val( ui.item.id );
                 return false;
        },
        change: function( event, ui ){
                    valid=true;
                    if ( !ui.item ) {
                      valid=false;
                    }
                   if ( !valid ) {
                     // remove invalid value, as it didn't match anything
                      $(this).val( "" );
                      $(this).data( "autocomplete" ).term = "";
                      return false;
                   }
                 return false;
        }
    });
    $('#add-matter-cancel').click(function(){
        $('#add-matter-popup').css('display', 'none');
        $('#facade').css('display', 'none');
    });

   $('#add-matter-submit').click(function(){
        var invalid = false;
        $('.validation-errors').empty();
        if($('#matter-category').val() == ""){
            $('.validation-errors').append('<span class="errors">* Category requires a value</span>');
            invalid = true;
        }
        if($('#matter-country').val() == ""){
            $('.validation-errors').append('<span class="errors">* Country requires a value</span>');
            invalid = true;
        }
        if($('#matter-caseref').val() == ""){
            $('.validation-errors').append('<span class="errors">* Caseref requires a value</span>');
            invalid = true;
        }
        if($('#matter-responsible').val() == ""){
            $('.validation-errors').append('<span class="errors">* Responsible requires a value</span>');
            invalid = true;
        }
        if(invalid)
            $('.validation-errors').css('display', 'block');
        else{
            $('.validation-errors').css('display', 'none');
            $.ajax({
                url: '/matter/add',
                type: 'POST',
                data: { category_code: $('#matter-category-code').val(),
                        country: $('#matter-country-id').val(),
                        origin: $('#matter-origin-id').val(),
                        type_code: $('#matter-type-id').val(),
                        caseref: $('#matter-caseref-id').val(),
                        responsible: $('#matter-responsible').val()
                      },
                success: function(data){
                    if(isNaN(parseInt(data))){
                       alert(data);
                    }else{
                        $('#add-matter-popup').css('display', 'none');
                        $('#facade').css('display', 'none');
                        $(location).attr("href", '/matter/view/id/'+data);
                    }
                }
            });
        }
   });
   $('#clone-matter-submit').click(function(){
        var invalid = false;
        $('.validation-errors').empty();
        if($('#matter-category').val() == ""){
            $('.validation-errors').append('<span class="errors">* Category requires a value</span>');
            invalid = true;
        }
        if($('#matter-country').val() == ""){
            $('.validation-errors').append('<span class="errors">* Country requires a value</span>');
            invalid = true;
        }
        if($('#matter-caseref').val() == ""){
            $('.validation-errors').append('<span class="errors">* Caseref requires a value</span>');
            invalid = true;
        }
        if($('#matter-responsible').val() == ""){
            $('.validation-errors').append('<span class="errors">* Responsible requires a value</span>');
            invalid = true;
        }
        if(invalid)
            $('.validation-errors').css('display', 'block');
        else{
            $('.validation-errors').css('display', 'none');
            $.ajax({
                url: '/matter/clone',
                type: 'POST',
                data: { category_code: $('#matter-category-code').val(),
                        country: $('#matter-country-id').val(),
                        origin: $('#matter-origin-id').val(),
                        type_code: $('#matter-type-id').val(),
                        caseref: $('#matter-caseref-id').val(),
                        responsible: $('#matter-responsible').val(),
                        matter_ID: $('#this-matter-id').val()
                      },
                success: function(data){
                    if( data != 'false' ){
                      //  alert("Matter added!");
                    $('#add-matter-popup').css('display', 'none');
                    $('#facade').css('display', 'none');
                        $(location).attr("href", '/matter/view/id/'+data);
                   }else
                      alert("Clone matter failed");
                }
            });
        }
   });
   $('#child-matter-submit').click(function(){
        var invalid = false;
        $('.validation-errors').empty();
        if($('#matter-country').val() == ""){
            $('.validation-errors').append('<span class="errors">* Country requires a value</span>');
            invalid = true;
        }
        if($('#matter-responsible').val() == ""){
            $('.validation-errors').append('<span class="errors">* Responsible requires a value</span>');
            invalid = true;
        }
        if(invalid)
            $('.validation-errors').css('display', 'block');
        else{
            $('.validation-errors').css('display', 'none');
            $.ajax({
                url: '/matter/child',
                type: 'POST',
                data: { category_code: $('#matter-category-code').val(),
                        country: $('#matter-country-id').val(),
                        origin: $('#matter-origin-id').val(),
                        type_code: $('#matter-type-id').val(),
                        caseref: $('#matter-caseref-id').val(),
                        responsible: $('#matter-responsible').val(),
                        matter_ID: $('#this-matter-id').val(),
                        priority: $('input[name=child-matter-priority]:checked').val(),
                        container: $('input[name=child-matter-container]:checked').val()
                      },
                success: function(data){
                    if( data != 'false' ){
                      //  alert("Matter added!");
                    $('#add-matter-popup').css('display', 'none');
                    $('#facade').css('display', 'none');
                        $(location).attr("href", '/matter/view/id/'+data);
                   }else
                      alert("Child matter failed");
                }
            });
        }
   });
   $("input:button, button").button();
});

</script>

<style type="text/css" >
#add-matter-head{
display:block;
width: 540px;
background: #1E4262;
color: #FFF;
padding: 5px;
border: 1px solid #1E4262;
-moz-border-radius: 5px 5px 0px 0px;
-webkit-border-radius: 5px 5px 0px 0px;
border-radius: 5px 5px 0px 0px;
}
#add-matter{
width: 540px;
height: auto;
background: whitesmoke;
display: block;
border: 1px inset #888;
padding: 5px;
-moz-border-radius: 0px 0px 5px 5px;
-webkit-border-radius: 0px 0px 5px 5px;
border-radius: 0px 0px 5px 5px;
}
#add-matter label{
display: inline-block;
text-align: right;
width: 75px;
margin: 0px 5px 10px 0px;
}
#child-matter-options {
border-top: 1px solid #DDD;
padding-top: 8px;
}
#child-matter-options span{
display:inline-block;
text-align: right;
width: 150px;
}
#child-matter-options label{
text-align: left;
width: 150px;
}
#add-matter input[type="text"]{
width: 170px;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
border: 1px inset #888;
padding: 3px 2px;
}
#add-matter-actions{
margin: 10px 0px 0px 0px;
float: right;
}

.required-field{
color: #f00;
    font-weight: bold;
    margin-right:4px;
}
.validation-errors{
display:none;
border:1px solid #f00;
color:#f00;
padding:5px;
width: auto;
    margin-bottom: 10px;
    -moz-border-radius: 5px;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
.validation-errors span{
display: block;
width: auto;
    margin-left: 20px;
}
.display-field{
display: inline-block;
width: 170px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    border-radius: 3px;
border: 1px solid #888;
padding: 1px 2px;
    vertical-align: top;
    margin-bottom: 2px;
    min-height: 14px;
}
</style>

<div class="place-card">
<div id="add-matter-head">
<?=$this->matter_title?>
</div>
<div id="add-matter">
<div class="validation-errors">

</div>
<form type="post" action="" id="add-matter-form" >

<label for="matter-category"><span class="required-field">*</span>Category</label>
<input type="text" name="matter-category" id="matter-category" value="<?=$this->category?>" class="required" onfocus="this.select()" />
<input type="hidden" name="matter-category-code" id="matter-category-code" value="<?=$this->category_code?>" />

<label for="matter-country"><span class="required-field">*</span>Country</label>
<input type="text" name="matter-country" id="matter-country" value="<?=$this->country_name?>" class="required" onfocus="this.select()" />
<input type="hidden" name="matter-country-id" id="matter-country-id" value="<?=$this->country_code?>" />

<label for="matter-origin">Origin</label>
<input type="text" name="matter-origin" id="matter-origin" value="<?=$this->origin_name?>" onfocus="this.select()" />
<input type="hidden" name="matter-origin-id" id="matter-origin-id" value="<?=$this->origin_code?>" />

<label for="matter-type">Type</label>
<input type="text" name="matter-type" id="matter-type" value="<?=$this->type_name?>" onfocus="this.select()" />
<input type="hidden" name="matter-type-id" id="matter-type-id" value="<?=$this->type_code?>" />

<label for="matter-caseref"><?php if(!isset($this->child)){?><span class="required-field">*</span><?php } ?>Caseref</label>
<?php if(isset($this->child)){?>
<span id="matter-caseref" class="display-field" ><?=$this->caseref?></span>
<?php }else { ?>
<input type="text" name="matter-caseref" id="matter-caseref" value="<?=$this->caseref?>" class="required" onfocus="this.select()" />
<?php } ?>
<input type="hidden" name="matter-caseref-id" id="matter-caseref-id" value="<?=$this->caseref?>" />

<label for="matter-responsible"><span class="required-field">*</span>Responsible</label>
<input type="text" name="matter-responsible" id="matter-responsible" value="<?=$this->username?>" class="required" onfocus="this.select()" />

<?php if(isset($this->child)){ ?>
<div id="child-matter-options" >
 <span>Current matter as a : </span> <input type="radio" name="child-matter-priority" value="1" checked="checked" id="current-priority-matter" /><label for="current-priority-matter" >PRIORITY</label>
  <input type="radio" name="child-matter-priority" value="0" id="current-parent-matter" /><label for="current-parent-matter">PARENT</label><br>
 <span>Child matter as : </span> <input type="radio" name="child-matter-container" value="1" id="child-independent-matter" /><label for="child-independent-matter" >Independent Container</label>
  <input type="radio" name="child-matter-container" value="0" checked="checked" id="child-inherit-matter" /><label for="child-inherit-matter">Inherits its information</label>
</div>
<?php } ?>

<div id="add-matter-actions">

<input type="button" name="<?=$this->matter_cap_id?>" id="<?=$this->matter_cap_id?>" value="<?=$this->matter_cap?>" />
<input type="button" name="add-matter-cancel" id="add-matter-cancel" value="Cancel" />
</div>
</form>
</div>
</div>
